package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.dao.LeaveDAOException;
import data.dto.LeaveDTO;
import data.dto.UserDTO;
import data.searchdto.LeaveSearchDTO;

import manager.LeaveManager;

/**
 * Servlet implementation class ManagePersonalLeaveServlet
 */
@WebServlet("/ManagePersonalLeaveServlet")
public class ManagePersonalLeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManagePersonalLeaveServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void processRequest(HttpServletRequest req, HttpServletResponse res)
			throws LeaveDAOException, ServletException, IOException,
			SQLException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(req.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		}
		else if (util.CurrentUser.isAdmin(user)){
			page = util.Constants.homePage;
		}
		else {

			LeaveManager leaveMgr = new LeaveManager();
			ArrayList<LeaveDTO> leavedata = leaveMgr.findAllLeave();
			LeaveSearchDTO search = new LeaveSearchDTO();
			search.setUserId(util.CurrentUser.getCurrentUser(req.getSession())
					.getId());

			Short type = null;
			if (null != req.getParameter("status"))
				type = Short.parseShort(req.getParameter("status"));
			else
				type = util.Constants.LEAVE_APPLICATION_STATUS_ALL;

			if (type == util.Constants.LEAVE_APPLICATION_STATUS_ALL) {
				try {
					leavedata = leaveMgr.findLeave(search);
				} catch (LeaveDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				search.setStatus(type);

				try {
					leavedata = leaveMgr.findLeave(search);
					// System.out.println("Leave Id"+leavedata.get(0));
				} catch (LeaveDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			page = "/staff/managePersonalLeave.jsp";
			req.setAttribute("currentstatus", type);
			req.setAttribute("leave", leavedata);
		}
		RequestDispatcher rd = req.getRequestDispatcher(page);
		rd.forward(req, res);

	}

}
